package com.biomatch.controller;

import com.biomatch.domain.UserInfo;
import com.biomatch.response.RestResponse;
import com.biomatch.response.RestResult;
import com.biomatch.service.PermissionService;
import com.biomatch.service.UserService;
import com.biomatch.utils.Internationalize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController("lock.login")
@RequestMapping("/login")
@Api(value="登录管理")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private Internationalize internationalize;

    @RequestMapping(value = "/loginByPassword", method = RequestMethod.POST)
    @ApiOperation(value = "根据账号密码登录", response = UserInfo.class, httpMethod = "POST")
    public RestResult<UserInfo> loginByPassword(@RequestBody UserInfo userInfo, HttpSession request){
        UserInfo userinfo = userService.loginByPassword(userInfo);
        if(userinfo == null){
            return RestResponse.error(internationalize.getMessage("login.password.wrong"));
        } else {
            return RestResponse.success(permissionService.getRoleAndPermissionByUserInfo(userinfo));
        }
    }

    @RequestMapping(value = "/getUserList", method = RequestMethod.POST)
    @ApiOperation(value = "获取用户列表", response = String[].class, httpMethod = "POST")
    public RestResult<List<UserInfo>> getUserList(){
        return RestResponse.success(userService.getUserList());
    }
}